<?php
namespace Attendance\Controller;
use Common\Controller\AdminbaseController;
use Home\Service\AccountService;
// +----------------------------------------------------------------------
// | Date:2018年3月26日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class ReclockController extends AdminbaseController{
    protected $model;
    public function _initialize(){
        $this->model=D('Attendance');
    }
    
    
    public function index(){
        $where['dev_code'] = 'sys';
        $order = 'id DESC';
        $list = $this->lists($this->model,$where,$order);
        $this->assign('list',$list);
        $this->display();
        
    }
    
    
    /**
     * 后台补打卡:
     * 同一个日期，只能补打卡一次，
     * 同一个员工，当月的1号至本月最后一天，只能进行补打卡三次操作
     * 
     * date:2018年3月26日
     * author: EK_熊
     */
    public function clock(){
        $name = I('name');
        $clocktime = I('clocktime');
        $type = I('type');
        if (IS_POST){
            
            if (empty($name)) $this->error('姓名不能为空');
            if (empty($clocktime)) $this->error('请填写打卡时间');
            if (empty($type)) $this->error('请选择打卡类型');
            
            $clocktime = $clocktime.':00';
            
            $accountService = new AccountService();
            $where['ep_name'] = ['like',"%$name%"];
            $where['status'] = 1;
            $userInfo = $accountService->get_user_by_field($where,'ep_name as name');
            
            $userInfo['start_time'] = $type == 1 ?  $clocktime : '';
            $userInfo['end_time'] = $type == 2 ?  $clocktime : '';
            $userInfo['name'] = $userInfo['ep_name'];
            $userInfo['uid'] = $userInfo['ep_id'];
            
            $attModel = D('Attendance');
            $clocktimeStmp = strtotime($clocktime);
            $getdate = getdate($clocktimeStmp);
            $date = date('Y-m-d',$clocktimeStmp);
 
            //检查目标日期所在月份的补打卡次数
            $reCount = $attModel->where([
                'sn'=>$userInfo['sn'],
                'year'=>$getdate['year'],
                'mon'=>$getdate['mon'],
                'dev_code'=>'sys'
                
            ])->count();
            if ($reCount >=3 ) $this->error("[{$getdate['mon']}月]补打卡次数达到上限(3次)");
            //检查记录是否存在
            $isexist = $attModel->where(['sn'=>$userInfo['sn'],'date'=>$date,'recept_time'=>$clocktime])->find();
            if ($isexist) $this->error("[{$clocktime}]已存在,请勿重复操作！！");
            //执行添加打卡点
            $numstr = $attModel->where(['dev_code'=>['like',"%fzzx_%"]])->order('dev_id desc')->getField('dev_id');
            $attendance = [
                'dev_id'=>$numstr,
                'name'=>$userInfo['name'],
                'sn'=>$userInfo['sn'],
                'time'=>$clocktime,
                'dev_code'=>'sys',
                'uid'=>$userInfo['uid']
            ];
            $appAtt = $attModel->write_to_db($attendance);
            if ($appAtt) {
                //更新attendance_day数据
                $repair = D('Approver')->solve_repair($userInfo);
                if (!$repair) $this->error('异常:打卡数据更新出错！');
                $this->success('添加成功！');
            }else{
                $this->error('异常:打卡点添加出错！');
            }

        }
        
        $this->display();
    }
    
}